﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Publisher_WS
{
    [Serializable]
    public class SessionInfo
    {
        private string sessionID;
        private int maKhachHang;
        private DateTime thoiGianLanCuoi;
        private GioHang gioHang;

        public GioHang GioHang
        {
            get { return gioHang; }
            set { gioHang = value; }
        }

        public SessionInfo(string sessionID, int customerID, DateTime dateTime)
        {
            this.sessionID = sessionID;
            this.maKhachHang = customerID;
            this.thoiGianLanCuoi = dateTime;
            gioHang = new GioHang();
            NapLaiGioHang(sessionID);
        }

        private void NapLaiGioHang(string sID)
        {
            List<PhieuMua> phieuMua = DataProvider
               .entities.PhieuMuas.Where(pm => pm.sID == sessionID).ToList();
            gioHang = new GioHang();
            foreach (var mua in phieuMua)
            {
                gioHang.DanhSachSach.Add(new SachTrongGio(mua.MaSach, mua.SoLuong));
            }
        }

        public string SessionId
        {
            get { return sessionID; }
            set { sessionID = value; }
        }

        public int MaKhachHang
        {
            get { return maKhachHang; }
            set { maKhachHang = value; }
        }

        public DateTime ThoiGianLanCuoi
        {
            get { return thoiGianLanCuoi; }
            set { thoiGianLanCuoi = value; }
        }

        public void HuyGioHang()
        {
            gioHang = new GioHang();
            List<PhieuMua> dsPm = DataProvider
                .entities.PhieuMuas.Where(s => s.sID == this.sessionID).ToList();
            foreach (var phieuMua in dsPm)
            {
                DataProvider.entities.PhieuMuas.DeleteObject(phieuMua);
            }
            DataProvider.entities.SaveChanges();
        }
    }
}